package com.starblink.login.forgotpassword.emailsent.ui

import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.guadou.lib_baselib.view.span.dsl.buildSpannableString
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.user.login.LoginManager
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.ActivityStackManage
import com.starblink.basic.util.CommUtils
import com.starblink.login.databinding.ActivityForgotPswEmailSentBinding

/**
 * 忘记密码-邮件已发送
 */
@Route(path = RoutePage.Login.FORGOT_PSW_EMAIL_SENT)
class ForgotPswEmailSentActivity :
    BaseTVMActivity<ActivityForgotPswEmailSentBinding, ForgotPswEmailSentVM>() {
    @Autowired(name = LoginManager.KEY_EMAIL)
    @JvmField
    var email: String? = null

    @Autowired(name = LoginManager.KEY_USERNAME)
    @JvmField
    var username: String? = null

    override fun initViewBinding(): ActivityForgotPswEmailSentBinding =
        ActivityForgotPswEmailSentBinding.inflate(layoutInflater)

    override fun initView() {
        super.initView()
        setEmail("***@***.***")
        viewBinding.tvTryAgain.buildSpannableString {
            addText("Didn't get the email? ") {
//                setColor(Color.parseColor("#1E293B"))
            }
            addText("Try again") {
                setColor(CommUtils.getColor(com.starblink.basic.style.R.color.sk_blue600))
                setClick(false) {
                    viewModel.sendResetPswEmail(email, username, manual = true)
                }
            }
        }

        viewBinding.btnContinue.click {
            // 结束当前页面和忘记密码输入邮箱页面，返回登录页
            // 路径1：登录页-邮箱登录-忘记密码
            // 路径2：登录页-绑定三方账号和Guang账号-忘记密码
            ActivityStackManage.finishLastActivities(3)
        }
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.sentEmailLiveData.observe(this) {
            setEmail(it)
        }
    }

    private fun setEmail(email: String) {
        viewBinding.tvEmail.buildSpannableString {
            addText("We sent an email to\n") {
//                setColor(Color.parseColor("#1E293B"))
            }
            addText("“$email”\n") {
                setColor(CommUtils.getColor(com.starblink.basic.style.R.color.sk_blue600))
            }
            addText("with password reset instructions.") {
//                setColor(Color.parseColor("#1E293B"))
            }
        }
    }

    override fun initLoad() {
        super.initLoad()
        viewModel.sendResetPswEmail(email, username)
    }
}